package cn.tkb.pet.controller;

import cn.tkb.basic.util.PageQuery;
import cn.tkb.pet.domain.Pet;
import cn.tkb.pet.domain.SearchMasterMsg;
import cn.tkb.pet.query.SearchMasterMsgQuery;
import cn.tkb.pet.service.ISearchMasterMsgService;
import cn.tkb.basic.exception.BusinessException;
import cn.tkb.basic.util.AjaxResult;
import cn.tkb.basic.util.LoginContext;
import cn.tkb.pet.domain.SearchMasterMsg;
import cn.tkb.pet.service.ISearchMasterMsgService;
import cn.tkb.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;

    /**
     * 寻主消息发布的接口
     *
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        //通过请求获取当前登录人的信息
        LoginInfo loginInfo = LoginContext.getCurrentLoginInfo(request);
        try {
            seachMasterMsgService.publish(searchMasterMsg, loginInfo);
            return AjaxResult.success();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("系统繁忙稍后重试！！！");
        }
    }

    /**
     * 处理寻主消息
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
             seachMasterMsgService.handle(pet,LoginContext.getCurrentLoginInfo(request));
             return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("处理失败！！");
        }
    }

    /**
     * 个人用户查询自己的发布消息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageQuery<SearchMasterMsg> userSearchMsg(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            return seachMasterMsgService.userSearchMsg(query,LoginContext.getCurrentLoginInfo(request));
        } catch (Exception e) {
            e.printStackTrace();
            return new PageQuery<>();
        }
    }


    /**
     * 查询待处理消息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageQuery<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            return seachMasterMsgService.pending(query,LoginContext.getCurrentLoginInfo(request));
        } catch (Exception e) {
            e.printStackTrace();
            return new PageQuery<>();
        }
    }

    /**
     * 查询已处理消息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageQuery<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            return seachMasterMsgService.processed(query,LoginContext.getCurrentLoginInfo(request));
        } catch (Exception e) {
            e.printStackTrace();
            return new PageQuery<>();
        }
    }
    

}